<template>
  <form
    class="detail-form"
    :model="form"
    :label-col="labelCol"
    :wrapper-col="wrapperCol"
  >
    <v-text-field allowClear label="账户名" v-model="form.username" />
    <v-text-field allowClear label="姓名" v-model="form.name" />
    <v-text-field
      allowClear
      label="密码"
      type="password"
      v-model="form.password"
    />
    <v-text-field allowClear label="手机号" v-model="form.mobile" />
    <v-select
      v-model="form.groupId"
      :items="groupList"
      item-text="title"
      item-value="id"
      label="所属角色"
    ></v-select>
    <v-btn small class="mr-2" @click="onSubmit">
      <v-icon class="d-none d-sm-flex" dark left
        >mdi-clipboard-edit-outline</v-icon
      >
      提交
    </v-btn>
  </form>
</template>
<script>
export default {
  data() {
    return {
      labelCol: { span: 5 },
      wrapperCol: { span: 14 },
      groupList: [],
      id: "",
      form: {
        username: "",
        groupId: "",
        password: "",
        name: "",
        mobile: "",
      },
    };
  },
  async created() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id;
      this.getData();
    }
    this.groupList = await this.Util.getRoleList();
  },
  methods: {
    // 获取数据
    getData() {
      this.pageLoading = true;
      this.Http.get("/admin/admin/detail", {
        id: this.id,
      }).then((res) => {
        this.pageLoading = false;
        if (res.code === 1) {
          res.data.password = "";
          this.form = res.data;
        } else {
          this.$Message.error(res.msg);
        }
      });
    },

    // 提交更新
    onSubmit() {
      console.log("submit!", this.form);
      let url, data;
      if (this.id) {
        url = "/admin/admin/update";
        data = {
          id: this.id,
          ...this.form,
        };
      } else {
        url = "/admin/admin/create";
        data = this.form;
      }
      this.Http.post(url, data).then((res) => {
        if (res.code == 1) {
          this.getData();
          this.$Message.success(res.msg);
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
  },
};
</script>
